{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "<h1> Car Lane Prediction </h1>\n",
    "\n",
    "<p align='justify'>\n",
    "\n",
    "This Jupyter notebook contains a classification example which is\n",
    "done with the help of Scikit-Learn library. In this notebook,\n",
    "the following steps are performed:\n",
    "</p>\n",
    "\n",
    "<ol align='justify'>\n",
    "    <li> The preprocessing i.e. feature generation, filtering and\n",
    "         interpolation of the data is carried out using the\n",
    "         PTRAIL Library.\n",
    "    </li>\n",
    "    <li> Further, several models like RandomForestClassifier, KMeans\n",
    "         Classifier etc. are trained using the Scikit-Learn library\n",
    "         based on the cleaned dataset.\n",
    "    </li>\n",
    "    <li>\n",
    "        Finally, on the interpolated dataset, the lane of the vehicle\n",
    "        is predicted.\n",
    "    </li>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "                                 vehicle_type  velocity  traffic_lane  \\\ntraj_id DateTime                                                        \n1371    1900-01-01 07:30:00.000             1      48.0             2   \n        1900-01-01 07:30:00.100             1      47.9             2   \n        1900-01-01 07:30:00.200             1      47.9             2   \n        1900-01-01 07:30:00.300             1      47.9             2   \n        1900-01-01 07:30:00.400             1      47.9             2   \n\n                                        lon        lat  kilopost  \\\ntraj_id DateTime                                                   \n1371    1900-01-01 07:30:00.000  135.469950  34.710999    3539.5   \n        1900-01-01 07:30:00.100  135.469957  34.710991    3532.5   \n        1900-01-01 07:30:00.200  135.469963  34.710984    3532.5   \n        1900-01-01 07:30:00.300  135.469968  34.710979    3531.5   \n        1900-01-01 07:30:00.400  135.469972  34.710974    3530.8   \n\n                                 vehicle_length  detected_flag  \ntraj_id DateTime                                                \n1371    1900-01-01 07:30:00.000             3.0              0  \n        1900-01-01 07:30:00.100             3.0              0  \n        1900-01-01 07:30:00.200             3.0              0  \n        1900-01-01 07:30:00.300             3.0              0  \n        1900-01-01 07:30:00.400             3.0              0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>vehicle_type</th>\n      <th>velocity</th>\n      <th>traffic_lane</th>\n      <th>lon</th>\n      <th>lat</th>\n      <th>kilopost</th>\n      <th>vehicle_length</th>\n      <th>detected_flag</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>DateTime</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">1371</th>\n      <th>1900-01-01 07:30:00.000</th>\n      <td>1</td>\n      <td>48.0</td>\n      <td>2</td>\n      <td>135.469950</td>\n      <td>34.710999</td>\n      <td>3539.5</td>\n      <td>3.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.100</th>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469957</td>\n      <td>34.710991</td>\n      <td>3532.5</td>\n      <td>3.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.200</th>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469963</td>\n      <td>34.710984</td>\n      <td>3532.5</td>\n      <td>3.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.300</th>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469968</td>\n      <td>34.710979</td>\n      <td>3531.5</td>\n      <td>3.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.400</th>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469972</td>\n      <td>34.710974</td>\n      <td>3530.8</td>\n      <td>3.0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Import the dataset.\n",
    "\n",
    "import pandas as pd\n",
    "from ptrail.core.TrajectoryDF import PTRAILDataFrame\n",
    "from datetime import datetime\n",
    "\n",
    "pdf = pd.read_csv('./data/traffic.csv')\n",
    "\n",
    "# Modifying the datetime values to make it correct format.\n",
    "lst = []\n",
    "for i in range(len(pdf)):\n",
    "    lst.append(pd.to_datetime(datetime.strptime(str(int(pdf.iloc[i]['datetime'])), \"%H%M%S%f\")))\n",
    "pdf['datetime'] = lst\n",
    "\n",
    "# Now converting the dataframe to PTRAILDataFrame.\n",
    "np_traffic = PTRAILDataFrame(data_set=pdf  ,\n",
    "                             latitude='lat',\n",
    "                             longitude='lon',\n",
    "                             datetime='datetime',\n",
    "                             traj_id='id')\n",
    "np_traffic.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of original DF: 44905\n",
      "Length of filtered DF: 44628\n",
      "CPU times: user 151 ms, sys: 112 ms, total: 263 ms\n",
      "Wall time: 3.45 s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yjharanwala/Desktop/PTRAIL/ptrail/preprocessing/filters.py:762: UserWarning: If kinematic features have been generated on the dataframe, then make sure to generate them again as outlier detection drops the point from the dataframe and does not run the kinematic features again.\n",
      "  warnings.warn(\"If kinematic features have been generated on the dataframe, then make \"\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# First, we will generate distance between consecutive points\n",
    "# and will run hampel filter on the basis of that.\n",
    "from ptrail.features.kinematic_features import KinematicFeatures\n",
    "from ptrail.preprocessing.filters import Filters\n",
    "\n",
    "np_traffic_dist = KinematicFeatures.create_distance_column(np_traffic)\n",
    "filt_traffic = Filters.hampel_outlier_detection(np_traffic_dist,\n",
    "                                                column_name='Distance')\n",
    "print(f\"Length of original DF: {len(np_traffic_dist)}\")\n",
    "print(f\"Length of filtered DF: {len(filt_traffic)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of original DF: 44628\n",
      "Length of filtered DF: 44628\n"
     ]
    }
   ],
   "source": [
    "# Now, lets drop duplicate points from the dataset.\n",
    "\n",
    "dp_traffic = Filters.remove_duplicates(filt_traffic)\n",
    "print(f\"Length of original DF: {len(filt_traffic)}\")\n",
    "print(f\"Length of filtered DF: {len(dp_traffic)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of original DF: 44628\n",
      "Length of filtered DF: 44626\n"
     ]
    }
   ],
   "source": [
    "# Finally, before interpolation, lets remove points\n",
    "few_filt_traffic = Filters.remove_trajectories_with_less_points(dp_traffic)\n",
    "print(f\"Length of original DF: {len(dp_traffic)}\")\n",
    "print(f\"Length of filtered DF: {len(few_filt_traffic)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "False    44435\nTrue       191\nName: DateTime, dtype: int64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Finally, lets interpolate the trajectory. We will use linear\n",
    "# interpolation this time to interpolate and make the trajectory\n",
    "# smoother.\n",
    "a = few_filt_traffic.reset_index()['DateTime'].diff().dt.total_seconds()\n",
    "(a > 0.1).value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(40275, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": "                                 index  vehicle_type  velocity  traffic_lane  \\\ntraj_id DateTime                                                               \n1389    1900-01-01 07:30:00.000   1603             1      28.6             2   \n        1900-01-01 07:30:00.100   1604             1      28.6             2   \n        1900-01-01 07:30:00.200   1605             1      29.2             2   \n        1900-01-01 07:30:00.300   1606             1      29.7             2   \n        1900-01-01 07:30:00.400   1607             1      30.3             2   \n\n                                        lon        lat  kilopost  \\\ntraj_id DateTime                                                   \n1389    1900-01-01 07:30:00.000  135.468358  34.712838    3790.2   \n        1900-01-01 07:30:00.100  135.468364  34.712832    3789.0   \n        1900-01-01 07:30:00.200  135.468370  34.712825    3788.0   \n        1900-01-01 07:30:00.300  135.468376  34.712818    3786.9   \n        1900-01-01 07:30:00.400  135.468381  34.712811    3785.9   \n\n                                 vehicle_length  detected_flag  Distance  \ntraj_id DateTime                                                          \n1389    1900-01-01 07:30:00.000             4.5              1       NaN  \n        1900-01-01 07:30:00.100             4.5              1  0.885661  \n        1900-01-01 07:30:00.200             4.5              1  0.900019  \n        1900-01-01 07:30:00.300             4.5              1  0.928737  \n        1900-01-01 07:30:00.400             4.5              1  0.937812  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>index</th>\n      <th>vehicle_type</th>\n      <th>velocity</th>\n      <th>traffic_lane</th>\n      <th>lon</th>\n      <th>lat</th>\n      <th>kilopost</th>\n      <th>vehicle_length</th>\n      <th>detected_flag</th>\n      <th>Distance</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>DateTime</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">1389</th>\n      <th>1900-01-01 07:30:00.000</th>\n      <td>1603</td>\n      <td>1</td>\n      <td>28.6</td>\n      <td>2</td>\n      <td>135.468358</td>\n      <td>34.712838</td>\n      <td>3790.2</td>\n      <td>4.5</td>\n      <td>1</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.100</th>\n      <td>1604</td>\n      <td>1</td>\n      <td>28.6</td>\n      <td>2</td>\n      <td>135.468364</td>\n      <td>34.712832</td>\n      <td>3789.0</td>\n      <td>4.5</td>\n      <td>1</td>\n      <td>0.885661</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.200</th>\n      <td>1605</td>\n      <td>1</td>\n      <td>29.2</td>\n      <td>2</td>\n      <td>135.468370</td>\n      <td>34.712825</td>\n      <td>3788.0</td>\n      <td>4.5</td>\n      <td>1</td>\n      <td>0.900019</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.300</th>\n      <td>1606</td>\n      <td>1</td>\n      <td>29.7</td>\n      <td>2</td>\n      <td>135.468376</td>\n      <td>34.712818</td>\n      <td>3786.9</td>\n      <td>4.5</td>\n      <td>1</td>\n      <td>0.928737</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.400</th>\n      <td>1607</td>\n      <td>1</td>\n      <td>30.3</td>\n      <td>2</td>\n      <td>135.468381</td>\n      <td>34.712811</td>\n      <td>3785.9</td>\n      <td>4.5</td>\n      <td>1</td>\n      <td>0.937812</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Based on the values shown above, the trajectory data is already\n",
    "# very smooth as is. Hence interpolation is not performed here. For\n",
    "# testing and training datasets, we will split 70% of the IDs for\n",
    "# training dataset and rest of the 30% for testing dataset.\n",
    "\n",
    "# 70% into the training dataset.\n",
    "ids_ = list(few_filt_traffic.traj_id.value_counts().keys())\n",
    "train_df = []\n",
    "for i in range(int(len(ids_) * 0.7)):\n",
    "    small = few_filt_traffic.reset_index().loc[few_filt_traffic.reset_index()['traj_id'] == ids_[i]]\n",
    "    train_df.append(small)\n",
    "\n",
    "np_train = PTRAILDataFrame(pd.concat(train_df),\n",
    "                           latitude='lat',\n",
    "                           longitude='lon',\n",
    "                           datetime='DateTime',\n",
    "                           traj_id='traj_id')\n",
    "print(np_train.shape)\n",
    "np_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4116, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": "                                 index  vehicle_type  velocity  traffic_lane  \\\ntraj_id DateTime                                                               \n1371    1900-01-01 07:30:00.000      0             1      48.0             2   \n        1900-01-01 07:30:00.100      1             1      47.9             2   \n        1900-01-01 07:30:00.200      2             1      47.9             2   \n        1900-01-01 07:30:00.300      3             1      47.9             2   \n        1900-01-01 07:30:00.400      4             1      47.9             2   \n\n                                        lon        lat  kilopost  \\\ntraj_id DateTime                                                   \n1371    1900-01-01 07:30:00.000  135.469950  34.710999    3539.5   \n        1900-01-01 07:30:00.100  135.469957  34.710991    3532.5   \n        1900-01-01 07:30:00.200  135.469963  34.710984    3532.5   \n        1900-01-01 07:30:00.300  135.469968  34.710979    3531.5   \n        1900-01-01 07:30:00.400  135.469972  34.710974    3530.8   \n\n                                 vehicle_length  detected_flag  Distance  \ntraj_id DateTime                                                          \n1371    1900-01-01 07:30:00.000             3.0              0       NaN  \n        1900-01-01 07:30:00.100             3.0              0  1.115504  \n        1900-01-01 07:30:00.200             3.0              0  0.939478  \n        1900-01-01 07:30:00.300             3.0              0  0.763477  \n        1900-01-01 07:30:00.400             3.0              0  0.596403  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>index</th>\n      <th>vehicle_type</th>\n      <th>velocity</th>\n      <th>traffic_lane</th>\n      <th>lon</th>\n      <th>lat</th>\n      <th>kilopost</th>\n      <th>vehicle_length</th>\n      <th>detected_flag</th>\n      <th>Distance</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>DateTime</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">1371</th>\n      <th>1900-01-01 07:30:00.000</th>\n      <td>0</td>\n      <td>1</td>\n      <td>48.0</td>\n      <td>2</td>\n      <td>135.469950</td>\n      <td>34.710999</td>\n      <td>3539.5</td>\n      <td>3.0</td>\n      <td>0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.100</th>\n      <td>1</td>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469957</td>\n      <td>34.710991</td>\n      <td>3532.5</td>\n      <td>3.0</td>\n      <td>0</td>\n      <td>1.115504</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.200</th>\n      <td>2</td>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469963</td>\n      <td>34.710984</td>\n      <td>3532.5</td>\n      <td>3.0</td>\n      <td>0</td>\n      <td>0.939478</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.300</th>\n      <td>3</td>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469968</td>\n      <td>34.710979</td>\n      <td>3531.5</td>\n      <td>3.0</td>\n      <td>0</td>\n      <td>0.763477</td>\n    </tr>\n    <tr>\n      <th>1900-01-01 07:30:00.400</th>\n      <td>4</td>\n      <td>1</td>\n      <td>47.9</td>\n      <td>2</td>\n      <td>135.469972</td>\n      <td>34.710974</td>\n      <td>3530.8</td>\n      <td>3.0</td>\n      <td>0</td>\n      <td>0.596403</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 30% into the testing dataset.\n",
    "test_df = []\n",
    "for i in range(int(len(ids_) * 0.7)+1, len(ids_)):\n",
    "    small = few_filt_traffic.reset_index().loc[few_filt_traffic.reset_index()['traj_id'] == ids_[i]]\n",
    "    test_df.append(small)\n",
    "\n",
    "np_test = PTRAILDataFrame(pd.concat(test_df),\n",
    "                          latitude='lat',\n",
    "                          longitude='lon',\n",
    "                          datetime='DateTime',\n",
    "                          traj_id='traj_id')\n",
    "print(np_test.shape)\n",
    "np_test.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "# Finally, splitting the test and train datasets as follows:\n",
    "#   1. Training:\n",
    "#       1.1 train_x\n",
    "#       1.2 train_y\n",
    "#   2. Testing:\n",
    "#       2.1 test_x\n",
    "#       2.2 test_y\n",
    "\n",
    "train_x = np_train.drop(columns=['traffic_lane', 'Distance'])\n",
    "train_y = np_train.reset_index()['traffic_lane']\n",
    "\n",
    "test_x = np_test.drop(columns=['traffic_lane', 'Distance'])\n",
    "test_y = np_test.reset_index()['traffic_lane']"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------- RandomForest Classifier -----------------\n",
      "The predicted train set values for RF are: [2 2 2 ... 1 1 1]\n",
      "The predicted test set values for RF are: [2 2 2 ... 1 1 1]\n",
      "\n",
      "The Training accuracy of RF is: 100.0%\n",
      "The Testing accuracy of RF is: 76.8%\n",
      "----------------------------------------------------------\n",
      "\n",
      "CPU times: user 3.38 s, sys: 16 ms, total: 3.4 s\n",
      "Wall time: 3.39 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now it is time to train some models and predict\n",
    "# the ship types.\n",
    "\n",
    "# 1. RandomForestClassifier model.\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "rf_model = RandomForestClassifier()\n",
    "rf_model.fit(train_x, train_y)\n",
    "\n",
    "# Now, lets predict on the training and testing set\n",
    "# using the above trained model.\n",
    "rf_train_predict = rf_model.predict(train_x)\n",
    "rf_test_predict = rf_model.predict(test_x)\n",
    "\n",
    "\n",
    "# Finally, lets test the accuracy of the model on both\n",
    "# the datasets.\n",
    "rf_train_accuracy = accuracy_score(train_y, rf_train_predict)\n",
    "rf_test_accuracy = accuracy_score(test_y, rf_test_predict)\n",
    "\n",
    "print('---------------- RandomForest Classifier -----------------')\n",
    "print(f\"The predicted train set values for RF are: {rf_train_predict}\")\n",
    "print(f\"The predicted test set values for RF are: {rf_test_predict}\\n\")\n",
    "print(f\"The Training accuracy of RF is: {round(rf_train_accuracy*100, 2)}%\")\n",
    "print(f\"The Testing accuracy of RF is: {round(rf_test_accuracy*100, 2)}%\")\n",
    "print('----------------------------------------------------------\\n')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------- DecisionTree Classifier -----------------\n",
      "The predicted train set values for DT are: [2 2 2 ... 1 1 1]\n",
      "The predicted test set values for DT are: [2 2 2 ... 1 1 1]\n",
      "\n",
      "The Training accuracy of DT is: 100.0%\n",
      "The Testing accuracy of DT is: 57.12%\n",
      "----------------------------------------------------------\n",
      "\n",
      "CPU times: user 113 ms, sys: 18 µs, total: 113 ms\n",
      "Wall time: 111 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# 2. DecisionTree Classifier model.\n",
    "\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "dt_model = DecisionTreeClassifier()\n",
    "dt_model.fit(train_x, train_y)\n",
    "\n",
    "# Now, lets predict on the training and testing set\n",
    "# using the above trained model.\n",
    "dt_train_predict = dt_model.predict(train_x)\n",
    "dt_test_predict = dt_model.predict(test_x)\n",
    "\n",
    "\n",
    "# Finally, lets test the accuracy of the model on both\n",
    "# the datasets.\n",
    "dt_train_accuracy = accuracy_score(train_y, dt_train_predict)\n",
    "dt_test_accuracy = accuracy_score(test_y, dt_test_predict)\n",
    "\n",
    "print('---------------- DecisionTree Classifier -----------------')\n",
    "print(f\"The predicted train set values for DT are: {dt_train_predict}\")\n",
    "print(f\"The predicted test set values for DT are: {dt_test_predict}\\n\")\n",
    "print(f\"The Training accuracy of DT is: {round(dt_train_accuracy*100, 2)}%\")\n",
    "print(f\"The Testing accuracy of DT is: {round(dt_test_accuracy*100, 2)}%\")\n",
    "print('----------------------------------------------------------\\n')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------- Naive Bayes Classifier -----------------\n",
      "The predicted train set values for GNB are: [2 2 2 ... 1 1 1]\n",
      "The predicted test set values for GNB are: [2 2 2 ... 1 1 1]\n",
      "\n",
      "The Training accuracy of GNB is: 68.25%\n",
      "The Testing accuracy of GNB is: 59.01%\n",
      "----------------------------------------------------------\n",
      "\n",
      "CPU times: user 26.7 ms, sys: 46 µs, total: 26.7 ms\n",
      "Wall time: 24.5 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# 3. Gaussian Naive Bayes model.\n",
    "\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "gnb_model = GaussianNB()\n",
    "gnb_model.fit(train_x, train_y)\n",
    "\n",
    "# Now, lets predict on the training and testing set\n",
    "# using the above trained model.\n",
    "gnb_train_predict = gnb_model.predict(X=train_x)\n",
    "gnb_test_predict = gnb_model.predict(test_x)\n",
    "\n",
    "\n",
    "# Finally, lets test the accuracy of the model on both\n",
    "# the datasets.\n",
    "gnb_train_accuracy = accuracy_score(train_y, gnb_train_predict)\n",
    "gnb_test_accuracy = accuracy_score(test_y, gnb_test_predict)\n",
    "\n",
    "print('----------------- Naive Bayes Classifier -----------------')\n",
    "print(f\"The predicted train set values for GNB are: {gnb_train_predict}\")\n",
    "print(f\"The predicted test set values for GNB are: {gnb_test_predict}\\n\")\n",
    "print(f\"The Training accuracy of GNB is: {round(gnb_train_accuracy*100, 2)}%\")\n",
    "print(f\"The Testing accuracy of GNB is: {round(gnb_test_accuracy*100, 2)}%\")\n",
    "print('----------------------------------------------------------\\n')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------- KNN Classifier ---------------------\n",
      "The predicted train set values for KNN are: [2 2 2 ... 1 1 1]\n",
      "The predicted test set values for KNN are: [2 2 2 ... 1 1 1]\n",
      "\n",
      "The Training accuracy of KNN is: 100.0%\n",
      "The Testing accuracy of KNN is: 35.86%\n",
      "----------------------------------------------------------\n",
      "\n",
      "CPU times: user 4.25 s, sys: 8 ms, total: 4.26 s\n",
      "Wall time: 4.24 s\n"
     ]
    }
   ],
   "source": [
    "\n",
    "%%time\n",
    "\n",
    "# 4. K-Nearest Neighbors Classifier model.\n",
    "\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "knn_model = KNeighborsClassifier()\n",
    "knn_model.fit(train_x, train_y)\n",
    "\n",
    "# Now, lets predict on the training and testing set\n",
    "# using the above trained model.\n",
    "knn_train_predict = knn_model.predict(train_x)\n",
    "knn_test_predict = knn_model.predict(test_x)\n",
    "\n",
    "\n",
    "# Finally, lets test the accuracy of the model on both\n",
    "# the datasets.\n",
    "knn_train_accuracy = accuracy_score(train_y, knn_train_predict)\n",
    "knn_test_accuracy = accuracy_score(test_y, knn_test_predict)\n",
    "\n",
    "print('--------------------- KNN Classifier ---------------------')\n",
    "print(f\"The predicted train set values for KNN are: {knn_train_predict}\")\n",
    "print(f\"The predicted test set values for KNN are: {knn_test_predict}\\n\")\n",
    "print(f\"The Training accuracy of KNN is: {round(knn_train_accuracy*100, 2)}%\")\n",
    "print(f\"The Testing accuracy of KNN is: {round(knn_test_accuracy*100, 2)}%\")\n",
    "print('----------------------------------------------------------\\n')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------- K-Means Classifier --------------------\n",
      "The predicted train set values for KM are: [2 2 2 ... 1 1 1]\n",
      "The predicted test set values for KM are: [2 2 2 ... 1 1 1]\n",
      "\n",
      "The Training accuracy of KM is: 46.56%\n",
      "The Testing accuracy of KM is: 43.42%\n",
      "----------------------------------------------------------\n",
      "\n",
      "CPU times: user 2.75 s, sys: 57.1 ms, total: 2.81 s\n",
      "Wall time: 310 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# 5. K-Means Classifier model.\n",
    "\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "km_model = KMeans(3)\n",
    "km_model.fit(train_x, train_y)\n",
    "\n",
    "# Now, lets predict on the training and testing set\n",
    "# using the above trained model.\n",
    "km_train_predict = km_model.predict(train_x)\n",
    "km_test_predict = km_model.predict(test_x)\n",
    "\n",
    "\n",
    "# Finally, lets test the accuracy of the model on both\n",
    "# the datasets.\n",
    "km_train_accuracy = accuracy_score(train_y, km_train_predict)\n",
    "km_test_accuracy = accuracy_score(test_y, km_test_predict)\n",
    "\n",
    "print('------------------- K-Means Classifier --------------------')\n",
    "print(f\"The predicted train set values for KM are: {km_train_predict}\")\n",
    "print(f\"The predicted test set values for KM are: {km_test_predict}\\n\")\n",
    "print(f\"The Training accuracy of KM is: {round(km_train_accuracy*100, 2)}%\")\n",
    "print(f\"The Testing accuracy of KM is: {round(km_test_accuracy*100, 2)}%\")\n",
    "print('----------------------------------------------------------\\n')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}